package com.example.demo.timu.tianyiyun;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: zjq
 * @Date: create in 2023/9/5 10:00
 */
@Api(tags = {"题库"})
@RestController
@RequestMapping("/tiku")
public class TikuController {


    private static final Logger log = LoggerFactory.getLogger(TikuController.class);
    @Autowired
    private TikuRepository tikuRepository;
    @Autowired
    private TikuService tikuService;

    //最简单的读取，excel中的数据列需要跟实体类一一对应
    @ApiOperation(value = "天翼云-写入数据库")
    @PostMapping("/upload")
    @ResponseBody
    public void upload(MultipartFile file) throws IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(file.getInputStream(), Tiku.class, new PageReadListener<Tiku>(dataList -> {
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            for (Tiku demoData : dataList) {
                log.info("读取到一条数据{}" + JSON.toJSONString(demoData));
                tikuRepository.save(demoData);
            }
        })).sheet().doRead();
    }

    @ApiOperation(value = "天翼云-模糊搜索题目")
    @GetMapping("/like")
    public Object like(String obj) {
        return tikuService.like(obj);
    }

    @ApiOperation(value = "删除重复的题目")
    @PostMapping("/deleted")
    public Object deleted() {
        return tikuService.deleted();
    }

}
